/****************************************************************
 * Copyright (c) 2006-2007 Besture All Rights Reserved.
 * 
 * File Name:		BasePage.cs
 * 
 * Created:	2013/06/24
 * Original Author:	Ni Tao
 * 
 * ------------------------------------------------------------
 * MODIFICATION HISTORY
 * ------------------------------------------------------------
 * When			Who		Why
 * ------------------------------------------------------------
 * 
 *
 ****************************************************************/
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using System.Text;

using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;

namespace Besture.Common
{   
    /// <summary>
    /// The class that involve Database,DbConnection,DbTransaction
    /// and manage these member to work
    /// </summary>
    class TransactionManager : CommonBase 
    {
        private Database cdbDatabase;
        private DbConnection cdbDbConnection;
        private DbTransaction cdbDbtransaction;
        public bool cbUseTransaction;
        public bool cbConnectionStatus;

        public TransactionManager()
        {
            cbUseTransaction = false;
            cbConnectionStatus = false;
        }
        public bool UseTransaction
        {
            get
            {
                return cbUseTransaction;
            }
            set
            {
                this.cbUseTransaction = value; 
            }
        }
        public bool ConnectionStatus
        {
            get
            {
                return cbConnectionStatus;
            }
            set
            {
                this.cbConnectionStatus = value;  
            }
        }
        public Database Database
        {
            get
            {
                return this.cdbDatabase;
            }
            set
            {
                this.cdbDatabase = value;
            }
        }
        public DbConnection DbConnection
        {
            get
            {
                return this.cdbDbConnection;
            }
            set
            {
                this.cdbDbConnection = value;
            }
        }
        public DbTransaction DbTransaction
        {
            get
            {
                return this.cdbDbtransaction;
            }
            set
            {
                this.cdbDbtransaction = value;
            }
        }

        /// <summary>
        /// Create connection with SQL server
        /// </summary>
        public void CreateConnection()
        {
            try
            {
                cdbDatabase = DatabaseFactory.CreateDatabase("ConnectionString");
                cdbDbConnection = cdbDatabase.CreateConnection();
            }
            catch (System.Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        /// <summary>
        /// Open connection with SQL server
        /// </summary>
        public void OpenConnection()
        {
            try
            {
                cdbDbConnection.Open();
            }
            catch (System.Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        /// <summary>
        /// Close connection with SQL server
        /// </summary>
        public void CloseConnection()
        {
            try
            {
                cdbDbConnection.Close();
            }
            catch (System.Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        /// <summary>
        /// Begin transaction by requirement
        /// </summary>
        public void BeginTransaction()
        {
            try
            {
                cdbDbtransaction = cdbDbConnection.BeginTransaction();
                cbUseTransaction = true;
            }
            catch (System.Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        /// <summary>
        /// Begin transaction by requirement
        /// </summary>
        public void BeginTransaction(IsolationLevel Level)
        {
            try
            {
                cdbDbtransaction = cdbDbConnection.BeginTransaction(Level);
                cbUseTransaction = true;
            }
            catch (System.Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        /// <summary>
        /// Commit the information updated
        /// </summary>
        public void Commit()
        {
            try
            {
                cdbDbtransaction.Commit();
            }
            catch (System.Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }
        
        /// <summary>
        /// Un do updating
        /// </summary>
        public void Rollback()
        {
            try
            {
                cdbDbtransaction.Rollback();
            }
            catch (System.Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }
    }
}
